package com.example.music;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.music.Bean.LocalMusicBean;
import com.example.music.DB.LoveInfo;
import com.example.music.Utils.ListDialog;
import com.example.music.Utils.LocalMusicAdapter;
import com.example.music.Utils.MusicPlayer;
import com.example.music.Utils.MusicPlaylist;
import com.example.music.Utils.MyViewModel;
import com.example.music.Utils.PlayListDialogFragment;
import com.example.music.Utils.songListAdapter;
import com.example.music.Utils.songListItem;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class ProfileActivity extends AppCompatActivity {

    LocalMusicAdapter localMusicAdapter;

    List<LocalMusicBean> list = new ArrayList<>();

    private SharedPreferences sharedPreferences;
    private MyViewModel viewModel;
    private LiveData<Boolean> dataLiveData;
    private ImageView home_btn;
    private ImageView profile_btn;
    private ImageView profile_img;  //头像
    private TextView profile_username;
    private ImageView center_btn;
    private LinearLayout profile_historyplay;
    private LinearLayout profile_collect;
    private LinearLayout profile_download;
    private LinearLayout profile_songdiets;
    private LinearLayout settings;
    private LinearLayout help;
    public void init(){
        profile_username = findViewById(R.id.profile_username);//用户名
        home_btn = findViewById(R.id.home_btn);
        profile_btn = findViewById(R.id.profile_btn);
        profile_img = findViewById(R.id.profile_img);
        center_btn = findViewById(R.id.center_btn);
        profile_historyplay = findViewById(R.id.profile_historyplay);
        profile_collect = findViewById(R.id.profile_collect);
        profile_download = findViewById(R.id.profile_download);
        profile_songdiets = findViewById(R.id.profile_songdiets);
        settings = findViewById(R.id.settings);
        help = findViewById(R.id.help);

        ImageView homeLostFocus = findViewById(R.id.home_btn);
        homeLostFocus.setImageResource(R.drawable.icon_home);
        ImageView profileFocus = findViewById(R.id.profile_btn);
        profileFocus.setImageResource(R.drawable.icon_foucs_profile);

        //sharePreferences获取登录状态
        sharedPreferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
        Boolean isLogin = sharedPreferences.getBoolean("isLogin",false);
        String curUserName = sharedPreferences.getString("curUserName","未登录");
        if(isLogin) {
            profile_img.setBackgroundResource(R.drawable.test);
            profile_username.setText(curUserName);
        }
    }
    public void initListener(){
        //个人主页
        profile_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (sharedPreferences.getBoolean("isLogin",false)) //已登录
                    jumpTo(myMessageActivity.class);
                else {
                    jumpTo(LoginActivity.class);
                }
            }
        });
        home_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {jumpTo(HomeActivity.class);}
        });
        center_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpTo(MainActivity.class);
            }
        });
        profile_historyplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpTo(profileSongHistoryActivity.class);
            }
        });
        profile_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {showToast("功能暂未上线！");}
        });
        profile_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {jumpTo(profileSongDownloadActivity.class);}
        });
        profile_songdiets.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showToast("功能暂未上线！");
            }
        });
        settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showToast("功能暂未上线！");
            }
        });
        help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ListDialog dialogFragment = new ListDialog();
                dialogFragment.show(getSupportFragmentManager(), "ListDialog");
            }
        });

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        init();
        initListener();

        list.clear();
        Integer curUserId = sharedPreferences.getInt("curUserId",-1);
        if(curUserId!=-1) {
            List<LoveInfo> LoveSongList = LitePal
                    .select("SongId")
                    .where("UserId = ?",String.valueOf(curUserId))
                    .find(LoveInfo.class);
            MusicPlaylist mData = MusicPlaylist.getInstance();
            for(int i=0;i<LoveSongList.size();i++){
                for(int j=0;j<mData.getSongs().size();j++){
                    if((LoveSongList.get(i).getSongId()+"").equals(mData.getSongs().get(j).getId())){
                        list.add(mData.getSongs().get(j));
                    }
                }
            }
            Log.e("个人页面喜欢列表",list.toString());
        }
        RecyclerView SongRecyclerView = findViewById(R.id.MyLoveRecyleView);
        RecyclerView.LayoutManager layoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        SongRecyclerView.setLayoutManager(layoutManager2);

        localMusicAdapter = new LocalMusicAdapter(this,list);
        SongRecyclerView.setAdapter(localMusicAdapter);

        setEventListener();
    }
    private void setEventListener() {
        /* 设置每一项的点击事件*/
        localMusicAdapter.setOnItemClickListener(new LocalMusicAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                MusicPlaylist mDatas = MusicPlaylist.getInstance();
                mDatas.setCurrentSongIndex(Integer.valueOf(list.get(position).getId())-1);
                LocalMusicBean musicBean = mDatas.getSongs().get(Integer.valueOf(list.get(position).getId())-1);
                MusicPlayer musicPlayer=MusicPlayer.getInstance();
                musicPlayer.addToPlaybackHistory(musicBean);
                musicPlayer.stop();
                musicPlayer.play(musicBean.getPath());
            }
        });
    }
    //跳转
    public void jumpTo(Class context){
        Intent intent = new Intent(ProfileActivity.this, context);
        startActivity(intent);
    }
    public void showToast(String msg) {
        // 创建一个Toast消息
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}